package com.syq.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import java.io.FileNotFoundException;

/**
 * @ClassName: ExceptionResolver
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/14 10:16
 */

@ControllerAdvice
public class ExceptionResolver {
    @ExceptionHandler(FileNotFoundException.class)
    public ModelAndView handlerException(FileNotFoundException fe){
        System.out.println(fe.getMessage());
        //
        ModelAndView mav = new ModelAndView();
        mav.addObject("error","注解式开发：：：离开了地球!不好意思");
        mav.setViewName("error");
        return mav;
    }
    //下面全是重载方法    就近分配  异常离哪个异常比较近   RuntimeException  ArithmeticException
    /**
     * 抛出算法异常的时候 执行下面的方法
     * @param ae
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    public ModelAndView handlerException(ArithmeticException ae){
        System.out.println(ae.getMessage());
        //
        ModelAndView mav = new ModelAndView();
        mav.addObject("error","注解式开发：：：离开了地球!不好意思");
        mav.setViewName("error");
        return mav;
    }
    /**
     * 抛出算法异常的时候 执行下面的方法
     * @param ae
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ModelAndView handlerException(Exception ae){
        System.out.println(ae.getMessage());
        //
        ModelAndView mav = new ModelAndView();
        mav.addObject("error","注解式开发：：：离开了地球!不好意思");
        mav.setViewName("error");
        return mav;
    }
}
